package com.xuelang.pipeline.artifact.entry.resource;

import com.alibaba.fastjson2.JSON;
import com.xuelang.pipeline.artifact.app.nexus3v1.enums.NexusWebhookEnum;
import com.xuelang.pipeline.artifact.app.nexus3v1.request.WebhookRepositoryAssetRequest;
import com.xuelang.pipeline.artifact.app.nexus3v1.request.WebhookRepositoryComponentRequest;
import com.xuelang.pipeline.artifact.app.nexus3v1.xo.ApiResponseXO;
import com.xuelang.pipeline.artifact.app.resource.CapabilityWebhookRepositoryService;
import com.xuelang.pipeline.artifact.app.resource.request.HarborEventRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Nexus3V1 仓库回调事件原生接口web服务
 *
 * @author xiwen.zxg
 * @since 2024/7/9
 */
@Slf4j
@RestController
@RequestMapping("/capability/webhook/repository")
public class CapabilityWebhookRepositoryController {

    @Autowired
    CapabilityWebhookRepositoryService capabilityWebhookRepositoryService;

    /**
     * 事件回调接收接口
     *
     * @param xRequestId            请求ID，可选参数
     * @param xWebhookSignature     Webhook签名，可选参数
     * @param xNexusWebhookDelivery Nexus Webhook 传输状态，可选参数
     * @param xNexusWebhookId       Nexus Webhook ID，可选参数
     * @param payload               回调负载数据，JSON格式
     * @return 返回API响应结果，无具体业务数据
     */
    @PostMapping(value = "/nexus", produces = "application/json;charset=utf-8")
    ApiResponseXO<Void> callbackNexusCapability(
            @RequestHeader(value = "X-Request-Id", required = false) String xRequestId,
            @RequestHeader(value = "X-Webhook-Signature", required = false) String xWebhookSignature,
            @RequestHeader(value = "X-Nexus-Webhook-Delivery", required = false) String xNexusWebhookDelivery,
            @RequestHeader(value = "X-Nexus-Webhook-Id", required = false) String xNexusWebhookId,
            @RequestBody(required = false) String payload) {
        log.info("\nHeaders >>> xRequestId:{}, xWebhookSignature:{}, xNexusWebhookDelivery:{}, xNexusWebhookId:{}; \nPayload >>> data:{}",
                xRequestId, xWebhookSignature, xNexusWebhookDelivery, xNexusWebhookId, payload);

        NexusWebhookEnum.EventTypeEnum nexusWebhookIdEnum = NexusWebhookEnum.EventTypeEnum.getByValue(xNexusWebhookId);
        switch (nexusWebhookIdEnum) {
            case RM_REPOSITORY_COMPONENT:
                capabilityWebhookRepositoryService.rmRepositoryComponent(JSON.parseObject(payload, WebhookRepositoryComponentRequest.class));
                break;
            case RM_REPOSITORY_ASSET:
                capabilityWebhookRepositoryService.rmRepositoryAsset(JSON.parseObject(payload, WebhookRepositoryAssetRequest.class));
                break;
            default:
                log.error("Nexus Webhook ID:{} is not supported", xNexusWebhookId);
                break;
        }
        return ApiResponseXO.success();
    }

    /**
     * 事件回调接收接口
     *
     * @return 返回API响应结果，无具体业务数据
     */
    @PostMapping(value = "/harbor", produces = "application/json;charset=utf-8")
    ApiResponseXO<Void> callbackHarborCapability(@RequestBody HarborEventRequest payload) {
        log.info("\nPayload >>> data:{}", JSON.toJSONString(payload));
        capabilityWebhookRepositoryService.processHarborArtifact(payload);
        return ApiResponseXO.success();
    }


}
